﻿/*!
* Ext JS Library 3.2.0
* Copyright(c) 2006-2010 Ext JS, Inc.
* licensing@extjs.com
* http://www.extjs.com/license
*/
//Ext.chart.Chart.CHART_URL = 'http://www.extjs.com/deploy/dev/resources/charts.swf';
Ext.chart.Chart.CHART_URL = '../ForeignFiles/ext/SWF/charts.swf';

(function () { 

    function _Chart(object,options){
        try{
            //Private Members
            var privateParameters;
            function _initialize(myDiv,options){
                try{
                    privateParameters = new PrivateParameters(options)
                    privateParameters.Version = new Object();
                    privateParameters.Version.Namespace = 'IndustrialDashboard';
                    privateParameters.Version.Class = 'Chart';
                    privateParameters.Version.info = 'ID 2.0';
                    privateParameters.Container = myDiv;
                    if (privateParameters.Events) {
                        if (privateParameters.Events.onError == undefined) privateParameters.Events.onError = function (error) { };
                    } else privateParameters.Events = {}; privateParameters.Events.onError = function (error) { alert(error); };

                }catch(Error){privateParameters.Events.onError(Error);}
            };
            
            function _refreshData(parameters){
                try{                            
                    var url = privateParameters.URL;
                    var options = {}; options.data = {};
                    options.data.DatabaseParameters = privateParameters.DatabaseParameters;
                    options.data.JSONFile = privateParameters.JSONFile;
                    for (var conParam in privateParameters.ConnectionParameters)
                        options[conParam] = privateParameters.ConnectionParameters[conParam];
                    if ((parameters) && (parameters.Version))
                        options.data.DatabaseParameters.QueryParameters = parameters.GetQueryParameters();
                    else options = (parameters) ? parameters : options;

                    options.data.Namespace = privateParameters.Version.Namespace;
                    options.data.Class = privateParameters.Version.Class;

                    options.onSuccess = function (requestData) {
                        if ((requestData) && (!requestData.message)) {   
                            privateParameters.Container.innerHTML ='';                         
                            privateParameters.Data = requestData;
                            _redraw(privateParameters.Container, privateParameters.Data);
                        }
                        else {                           
                            privateParameters.Container.innerHTML = '';
                        }
                    };

                    options.onFailure = function (args) {
                        privateParameters.Events.onError(args);
                    }
                    RequestJSON(url, options);
                }catch(Error){privateParameters.Events.onError(Error);}
            };
            function _redraw(container,data){
                try{
                    if ((container)&&(data)&&(data.data.length)){ 
                        privateParameters.Container.innerHTML ='';                                                                                                  
                        _ie2(container,data);
                    }else{
                        privateParameters.Container.innerHTML ='';
                        var loadingContainer = document.createElement('div');
                        loadingContainer.style.top = '200px';
                        loadingContainer.style.marginLeft = '-50px';
                        var loadingOptions = { 'Type': 0, 'Title': 'No Available Data.', 'Animate': false };                        
                        loading = new DisplayPoster(loadingContainer, loadingOptions);
                        loading.SetVisible(true);
                        privateParameters.Container.appendChild(loadingContainer);
                    }
                }catch(Error){privateParameters.Events.onError(Error);}
            };                      
            function _ie2(container,data){
                try{
                    var store = new Ext.data.JsonStore(data);
                    new Ext.Panel({
                    iconCls: 'chart',
                    title: '('+data.fields[1].toString() +' vs '+ data.fields[0].toString()+') By ' + data.fields[2].toString(),
                    frame: true,
                    renderTo: container.id,
                    width: 850,
                    height: 350,
                    layout: 'fit',

                    items: {
                        xtype: 'columnchart',
                        store: store,
                        url: 'http://www.extjs.com/deploy/dev/resources/charts.swf',
                        xField: data.fields[2].toString(),
                        yAxis: new Ext.chart.NumericAxis({
                            displayName: '('+data.fields[1].toString() +' vs '+ data.fields[0].toString()+') By ' + data.fields[2].toString(),
                            labelRenderer: Ext.util.Format.numberRenderer('0,0')
                        }),
                        tipRenderer: function (chart, record, index, series) {
                            if (series.yField == data.fields[1].toString()) {
                                return Ext.util.Format.number(eval('record.data.'+data.fields[1].toString()), '0,0') + ' [Tons] ' +data.fields[1].toString() +' at ' + eval('record.data.'+data.fields[2].toString());
                            } else {
                                return Ext.util.Format.number(eval('record.data.'+data.fields[0].toString()), '0,0')  + ' [Grade] ' +data.fields[0].toString() +' at '+ eval('record.data.'+data.fields[2].toString());
                            }
                        },
                        chartStyle: {
                            padding: 10,
                            animationEnabled: true,
                            font: {
                                name: 'Tahoma',
                                color: 0x444444,
                                size: 10
                            },
                            dataTip: {
                                padding: 5,
                                border: {
                                    color: 0x99bbe8,
                                    size: 1
                                },
                                background: {
                                    color: 0xDAE7F6,
                                    alpha: .9
                                },
                                font: {
                                    name: 'Tahoma',
                                    color: 0x15428B,
                                    size: 10,
                                    bold: true
                                }
                            },
                            xAxis: {
                                color: 0x69aBc8,
                                majorTicks: { color: 0x69aBc8, length: 4 },
                                minorTicks: { color: 0x69aBc8, length: 2 },
                                majorGridLines: { size: 1, color: 0xeeeeee }
                            },
                            yAxis: {
                                color: 0x69aBc8,
                                majorTicks: { color: 0x69aBc8, length: 4 },
                                minorTicks: { color: 0x69aBc8, length: 2 },
                                majorGridLines: { size: 1, color: 0xdfe8f6 }
                            }
                        },
                        series: [{
                            type: 'line',                            
                            displayName: data.fields[1].toString(),
                            yField: data.fields[1].toString(),
                            style: {
                                color: 0xE66721,
                                size: 7,
                                lineSize: 3,
                                fillColor   : 0xF5E7E1,
                                borderColor : 0xF75506,
                                lineColor   : 0xF75506                                
                            }
                        }, {
                            type: 'line',
                            displayName: data.fields[0].toString(),
                            yField: data.fields[0].toString(),
                            style: {
                                color: 0x15428B,
                                size: 7,
                                lineSize: 3,
                                borderSize: 2,
                                fillColor   : 0xBFC7D6,
                                borderColor : 0x15428B,
                                lineColor   : 0x15428B
                            }
                        }]
                    }
                });
                }catch(Error){privateParameters.Events.onError(Error);}
            };

            _initialize(object,options);

            //Public Members
            this.GetDatabaseEngine = function () { return DatabaseConnection.GetDatabaseEngine(privateParameters.DatabaseEngine); };
            this.SetDatabaseEngine = function (newDatabaseEngine) { DatabaseConnection.SetDatabaseEngine(privateParameters, newDatabaseEngine); };
            this.GetConnectionStringName = function () { return DatabaseConnection.GetConnectionStringName(privateParameters.ConnectionStringName); };
            this.SetConnectionStringName = function (newConnectionStringName) { DatabaseConnection.SetConnectionStringName(privateParameters, newConnectionStringName); };
            this.GetProcedure = function () { return DatabaseConnection.GetProcedure(privateParameters.Procedure); };
            this.SetProcedure = function (newProcedure) { DatabaseConnection.SetProcedure(privateParameters, newProcedure); };
            this.GetTablesCount = function () { return DatabaseConnection.GetTablesCount(privateParameters.TablesCount); };
            this.SetTablesCount = function (newTablesCount) { DatabaseConnection.SetTablesCount(privateParameters, newTablesCount); };
            this.RefreshData= function(parameters){
                if (parameters) _refreshData(parameters);
            }
        }catch(Error){privateParameters.Events.onError(Error);}
    };

    window.Chart = function(object,options){return new _Chart(object,options);}

})();
